/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial gpsPort;    // Create object from Serial class for the GPS port
Serial geigerPort; // Create object from Serial class for the Geiber port

// log file
PrintWriter logFile;

// startup count
int start_up_count = 0;

// Geiger related variable
String CPM = "0";        // Counts per minutes from Geiger counter

// GPA related variables
String gpsDataArray[];
String data;
Float Latitude1 = 0.0;
String Latitude;
String NorSindicator;
Float Longitude1 = 0.0;
String Longitude;
String EorWindicator;
String Satellites;
String Precision;
String  Altitude;
String gpsdata;
String time;
String Quality;
int GPSDataReady = 1;

void setup() 
{
  // Set up window
  size(800, 200); //window size
  PFont myFont = createFont(PFont.list()[2], 16); // font type
  textFont(myFont);

  noStroke();
  smooth();
  
  // open file
  String filename = "/Users/robinscheibler/safecast-" + year() + month() + day() + "-" + hour() + minute() + ".log";
  logFile = createWriter(filename);

  // Setting up Serial communication
  println(Serial.list());
  // Open serial port for GPS dongle
  String gpsPortName = Serial.list()[0];
  gpsPort = new Serial(this, gpsPortName, 38400);
  // Open serial port for the Geiger-arduino
  String geigerPortName = Serial.list()[2];
  geigerPort = new Serial(this, geigerPortName, 9600);
}

void draw()
{
  background(0);
  fill(255);
  if ( geigerPort.available() > 0) {  // If data is available,
    CPM = geigerPort.readStringUntil('\n');         // read it and store it in val
    gpsPort.clear();
    readGPS(gpsPort);
    gpsPort.clear();
    // wait 12 counts in order for the Geiger to stabilize
    if (start_up_count >= 12)
    {
      logFile.print(day() + ":" + minute() + ":" + second() + "," + CPM + ",");
      logFile.print(Latitude + "," + NorSindicator + "," + nfs(Latitude1,2,6) + ",");
      logFile.print(Longitude + "," + EorWindicator + "," + nfs(Longitude1,3,6) + ",");
      logFile.println(Quality + "," + Satellites + "," + Precision + "," + Altitude);
      logFile.flush();
    } else {
      // increase counter at startup
      start_up_count++;
    }
    
  }
  // window display
  textAlign(LEFT);
  text("Time: " + day() + ":" + minute() + ":" + second() + ":" + " CPM: " + CPM, 20, 30);
  text("Latitude: " + Latitude1 + " Longitude: " + Longitude1, 20, 50);
  text("Position: " + NorSindicator + "   " + EorWindicator, 20, 70);
  text("Satellites Used:  "+ Satellites, 20, 90);
  text("Altitude: "+ Altitude + " Meters Precision: " + Precision, 20, 110);
  text("GPS unparsed: "+ gpsdata, 20, 130);
}

// function reads the gps data and sends it to the parsing function
void readGPS(Serial gpsPort) {
  int gotPosition = 0; 
  while (gotPosition == 0)
  {
    while (gpsPort.available () <= 0);
    String data = gpsPort.readStringUntil('\n');
    if ( data != null) {
      gotPosition = parseString(data);
    }
  }
}

//parsing function
int parseString (String serialString) {
  //sets up an array and splits the gps info by comma
  String items[] = split(serialString, ',');
  if (items[0].equals("$GPGGA") && items.length >= 9) {  //looks for the GPGGA info
    getGGA(items);
    return 1;
  } else {
    return 0;
  }
}
//this function assigns the variables
//to array elements that have been parsed
void getGGA(String[] gpsinfo) {
  time = gpsinfo[1];
  float LatDeg = floor(float(gpsinfo[2])/100.0);
  float LatMin = float(gpsinfo[2]) - LatDeg*100.0;
  Latitude1 = LatDeg + LatMin/60.0;
  Latitude = gpsinfo[2];
  NorSindicator = gpsinfo[3];
  float LonDeg = floor(float(gpsinfo[4])/100.0);
  float LonMin = float(gpsinfo[4]) - LonDeg*100.0;
  Longitude1 = LonDeg + LonMin/60.0;
  Longitude = gpsinfo[4];
  EorWindicator = gpsinfo[5];
  Quality = gpsinfo[6];
  Satellites = gpsinfo[7];
  Precision = gpsinfo[8];
  Altitude = gpsinfo[9];
  gpsdata = join(gpsinfo, ",");
}

